
package restaurante.cih;


import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import restaurante.cci.CtrlCliente;
import restaurante.cdp.ClienteDomiciliar;
import restaurante.util.Rest.tipo;

public class JanFormularioClienteDomiciliar extends javax.swing.JFrame {

    private static JanFormularioClienteDomiciliar instance;
    private CtrlCliente c;
    List<ClienteDomiciliar> listClienteDomiciliar = new ArrayList<>();
    private static JanEventosClienteDomiciliar janEventosClienteDomiciliar;
    private tipo MODO;
    private ClienteDomiciliar clienteDomiciliar;
    

    public tipo getMODO() {
        return MODO;
    }

    public void setMODO(tipo MODO) {
        this.MODO = MODO;
    }

    public List<ClienteDomiciliar> getListClienteDomiciliar() {
        return listClienteDomiciliar;
    }

    public void setListClienteDomiciliar(List<ClienteDomiciliar> listClienteDomiciliar) {
        this.listClienteDomiciliar = listClienteDomiciliar;
    }

    
    
    /**
     * Creates new form JanFormularioEmpresas
     */
    public static JanFormularioClienteDomiciliar getInstance() throws Exception{
        if(instance == null){
            instance = new JanFormularioClienteDomiciliar();
            janEventosClienteDomiciliar = JanEventosClienteDomiciliar.getInstance(); 
            
        }
        return instance;
    }
    
    private JanFormularioClienteDomiciliar() throws Exception {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPanel12 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel11 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jFormattedTextField1 = new javax.swing.JFormattedTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jFormattedTextField3 = new javax.swing.JFormattedTextField();
        jLabel7 = new javax.swing.JLabel();
        jFormattedTextField2 = new javax.swing.JFormattedTextField();
        jLabel9 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();

        jLabel3.setText("jLabel3");

        setTitle("Cadastro");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Cliente Domiciliar");
        getContentPane().add(jLabel1, java.awt.BorderLayout.PAGE_START);

        jButton1.setText("Salvar");
        jButton1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel12.add(jButton1);

        jButton2.setText("Voltar");
        jButton2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jButton2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel12.add(jButton2);

        getContentPane().add(jPanel12, java.awt.BorderLayout.PAGE_END);

        jPanel1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jPanel11.setLayout(new java.awt.GridLayout(7, 2));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Nome: ");
        jPanel11.add(jLabel2);
        jPanel11.add(jTextField1);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("CPF:");
        jPanel11.add(jLabel4);

        jFormattedTextField1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        jPanel11.add(jFormattedTextField1);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("Logradouro:");
        jPanel11.add(jLabel5);

        jTextField3.setMinimumSize(new java.awt.Dimension(40, 20));
        jPanel11.add(jTextField3);

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel8.setText("Bairro:");
        jPanel11.add(jLabel8);

        jTextField4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField4ActionPerformed(evt);
            }
        });
        jPanel11.add(jTextField4);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText("Número:");
        jPanel11.add(jLabel6);

        jFormattedTextField3.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        jPanel11.add(jFormattedTextField3);

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("CEP:");
        jPanel11.add(jLabel7);

        jFormattedTextField2.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        jPanel11.add(jFormattedTextField2);

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel9.setText("Complemento:");
        jPanel11.add(jLabel9);

        jTextField7.setMinimumSize(new java.awt.Dimension(40, 20));
        jPanel11.add(jTextField7);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addComponent(jPanel11, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                .addGap(96, 96, 96))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jPanel11, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(27, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        List componentes = new ArrayList();
        componentes.add(this.jTextField1.getText());
        componentes.add(this.jFormattedTextField1.getText());
        componentes.add(this.jTextField3.getText());
        componentes.add(this.jTextField4.getText());
        componentes.add(this.jFormattedTextField3.getText());
        componentes.add(this.jFormattedTextField2.getText());
        componentes.add(this.jTextField7.getText());   
        acaoSalvar(componentes);               
            
            
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        acaoVoltar();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        try {  
            c = CtrlCliente.getInstance();
        } catch (Exception ex) {
            Logger.getLogger(JanFormularioClienteDomiciliar.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_formWindowOpened

    private void jTextField4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField4ActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        acaoVoltar();
    }//GEN-LAST:event_formWindowClosed


    private void formWindowDeactivated(java.awt.event.WindowEvent evt) {
        acaoVoltar();
    }
     
    
     
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton jButton1;
    public javax.swing.JButton jButton2;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JFormattedTextField jFormattedTextField2;
    private javax.swing.JFormattedTextField jFormattedTextField3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    public javax.swing.JTextField jTextField1;
    public javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    public javax.swing.JTextField jTextField7;
    // End of variables declaration//GEN-END:variables

    public void acaoIncluir() {
        arrumarTela(true);
        jButton1.setVisible(true);
        setVisible(true);
    }

    public void arrumarTela(boolean b) {

        jTextField1.setEnabled(b);
        jFormattedTextField1.setEnabled(b);
        jTextField3.setEnabled(b);
        jTextField4.setEnabled(b);
        jFormattedTextField3.setEnabled(b);
        jFormattedTextField2.setEnabled(b);
        jTextField7.setEnabled(b);

    }

    public void acaoSalvar(List componentes) {
        String msg = validaCampos();
        if(msg.equals(""))
        {
            if(validarTextosFormatados())
            {
                if (MODO != tipo.INCLUIR) {
                    c.alterarClienteDomiciliar(componentes, clienteDomiciliar);
                } 
                else {

                    limparTela();

                    clienteDomiciliar = c.incluirClienteDomiciliar(componentes);
                }
                setVisible(false);

                c.dadosClienteDomiciliar(janEventosClienteDomiciliar.jTable1.getModel(), listClienteDomiciliar.size());
            }
        }
        else
        {
            JOptionPane.showMessageDialog(this, msg, "Informe: ",JOptionPane.WARNING_MESSAGE);
        }
    }

    public void limparTela() {
        jTextField1.setText("");
        jFormattedTextField1.setText("");
        jTextField3.setText("");
        jTextField4.setText("");
        jFormattedTextField3.setText("");
        jFormattedTextField2.setText("");
        jTextField7.setText("");
    }

    public void acaoVoltar() {
        limparTela();
        setVisible(false);
    }


    public void acaoAlterar(int linhaSelecionada) {
        ClienteDomiciliar cD = listClienteDomiciliar.get(linhaSelecionada);
        montarTela(cD);
        clienteDomiciliar = cD;
        arrumarTela(true);
        jButton1.setVisible(true);
        setVisible(true);
    }

    public void montarTela(ClienteDomiciliar cD) {
        jTextField1.setText(cD.getNome());
        jFormattedTextField1.setText(cD.getCpf().toString());
        jTextField3.setText(cD.getEndereco().getLogradouro());
        jTextField4.setText(cD.getEndereco().getBairro());
        jFormattedTextField3.setText(cD.getEndereco().getNumero().toString());
        jFormattedTextField2.setText(cD.getEndereco().getCep().toString());
        jTextField7.setText(cD.getEndereco().getComplemento());
    }

    public void acaoConsultar(int selectedRow) {
        clienteDomiciliar = listClienteDomiciliar.get(selectedRow);
        montarTela(clienteDomiciliar);
        arrumarTela(false);
        jButton1.setVisible(false);
        setVisible(true);
    }
    
    public String validaCampos()
    {
        String msg = "";
        if(jTextField1.getText().equals(""))
        {
            msg+= "-Nome\n";
        }
        if(jFormattedTextField1.getText().equals(""))
        {
            msg+= "-CPF\n";
        }
        if(jTextField3.getText().equals(""))
        {
            msg+= "-Logradouro\n";
        }
        if(jTextField4.getText().equals(""))
        {
            msg+= "-Bairro\n";
        }
        if(jFormattedTextField3.getText().equals(""))
        {
            msg+= "-Número\n";
        }
        if(jFormattedTextField2.getText().equals(""))
        {
            msg+= "-CEP\n";
        }
        return msg;
    }
    
    public boolean validarTextosFormatados()
    {
        if(((Long.valueOf((String) jFormattedTextField1.getText()))<0))
        {
            JOptionPane.showMessageDialog(this, "CPF Inválido!", "Informe: ",JOptionPane.WARNING_MESSAGE);
            return false;
        }
        else if((Long.valueOf((String) jFormattedTextField2.getText()))<0)
        {
            JOptionPane.showMessageDialog(this, "CEP Inválido!", "Informe: ",JOptionPane.WARNING_MESSAGE);
            return false;
        }
        else if((Integer.valueOf((String) jFormattedTextField3.getText()))<0)
        {
            JOptionPane.showMessageDialog(this, "Número Inválido!", "Informe: ",JOptionPane.WARNING_MESSAGE);
            return false;
        }
        return true;
    }
}

